package com.tech.koufu.ui.view;

import com.tech.koufu.R;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

/**
 * 切换Fragment
 * @author 123
 *
 */
public class BaseContainerFragment extends Fragment {

    public void replaceFragment(Fragment fragment, boolean addToBackStack,String tag) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(R.id.container_framelayout, fragment,tag);
        transaction.commitAllowingStateLoss();
        getChildFragmentManager().executePendingTransactions();
    }

    public boolean popFragment() {
        Log.e("ttest", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
        boolean isPop = false;
        if (getChildFragmentManager().getBackStackEntryCount() > 0) {
            isPop = true;
            getChildFragmentManager().popBackStack();
        }
        return isPop;
    }

    public void replaceFragment(int containerlayid,Fragment fragment, boolean addToBackStack) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(containerlayid, fragment);
        transaction.commitAllowingStateLoss();
        getChildFragmentManager().executePendingTransactions();
    }
    //~~~~~~~~~~need 
    public void addFragment(int containerlayid,Fragment fragment) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
        transaction.add(containerlayid, fragment); 
        transaction.commitAllowingStateLoss();
        getChildFragmentManager().executePendingTransactions();
    }
    /**
     * 
     * @param fragment
     */
    public void hideFragment(Fragment fragment){
    	if(fragment==null)
    		return;
    	 FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
    	 transaction.hide(fragment);
    	 transaction.commit();
    	 getChildFragmentManager().executePendingTransactions();
    }
    public void showFragment(Fragment fragment){
    	if(fragment==null)
    		return;
    	 FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
    	 transaction.show(fragment);
    	 transaction.commit();
    	 getChildFragmentManager().executePendingTransactions();
    }
}